// pages/profile/profile.js
import { photoService } from '../../services/photo'

Page({
  data: {
    userInfo: null,
    stats: {
      openid: '',
      totalLikes: 0,
      totalPhotos: 0
    },
    leftPhotos: [],    // 左列照片
    rightPhotos: [],   // 右列照片
    page: 1,
    loading: false,
    noMore: false
  },

  onLoad() {
    const userInfo = wx.getStorageSync('userInfo')
    if (userInfo) {
      this.setData({ userInfo })
      this.loadUserData()
    }
  },

  // 加载用户数据
  async loadUserData() {
    try {
      this.setData({ loading: true })
      
      // 获取用户统计数据
      const stats = await photoService.getUserStats(this.data.userInfo.id)
      this.setData({ stats })
      
      // 加载用户照片
      await this.loadUserPhotos()
    } catch (error) {
      console.error('加载用户数据失败:', error)
      wx.showToast({
        title: '加载失败',
        icon: 'none'
      })
    } finally {
      this.setData({ loading: false })
    }
  },

  // 加载用户照片
  async loadUserPhotos() {
    if (this.data.loading || this.data.noMore) return
    
    try {
      const res = await photoService.getUserPhotos(this.data.userInfo.id, this.data.page)
      
      // 将新照片分配到左右两列
      const { leftPhotos, rightPhotos } = this.data
      res.list.forEach((photo, index) => {
        if (index % 2 === 0) {
          leftPhotos.push(photo)
        } else {
          rightPhotos.push(photo)
        }
      })

      this.setData({
        leftPhotos,
        rightPhotos,
        page: this.data.page + 1,
        noMore: !res.hasMore
      })
    } catch (error) {
      console.error('加载照片失败:', error)
      wx.showToast({
        title: '加载失败',
        icon: 'none'
      })
    }
  },

  // 删除照片
  async handleDeletePhoto(e) {
    const { id } = e.currentTarget.dataset
    
    try {
      await wx.showModal({
        title: '确认删除',
        content: '确定要删除这张照片吗？'
      })
      
      await photoService.deletePhoto(id)
      
      // 从列表中移除已删除的照片
      const leftPhotos = this.data.leftPhotos.filter(photo => photo.id !== id)
      const rightPhotos = this.data.rightPhotos.filter(photo => photo.id !== id)
      this.setData({ 
        leftPhotos,
        rightPhotos,
        'stats.totalPhotos': this.data.stats.totalPhotos - 1
      })
      
      wx.showToast({
        title: '删除成功',
        icon: 'success'
      })
    } catch (error) {
      console.error('删除失败:', error)
      wx.showToast({
        title: '删除失败',
        icon: 'none'
      })
    }
  },

  // 预览照片
  previewPhoto(e) {
    const { url } = e.currentTarget.dataset
    const urls = [...this.data.leftPhotos, ...this.data.rightPhotos].map(photo => photo.imagePath)
    wx.previewImage({
      current: url,
      urls
    })
  },

  // 下拉刷新
  async onPullDownRefresh() {
    this.setData({
      leftPhotos: [],
      rightPhotos: [],
      page: 1,
      noMore: false
    })
    await this.loadUserData()
    wx.stopPullDownRefresh()
  },

  // 触底加载更多
  onReachBottom() {
    if (!this.data.noMore) {
      this.loadUserPhotos()
    }
  }
})